<?php


namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;

class merchantModel extends Model
{
    protected $table = 'merchant';
    protected $primaryKey = 'id';
    protected $appends = ['status_name', 'root_name'];
    protected $hidden = [];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getStatusNameAttribute($value)
    {
        $status_name = '禁用';

        if ($this->attributes['status'] > 0) {
            $status_name = '正常';
        }

        return $status_name;
    }

    public function getRootNameAttribute($value)
    {
        $root_name = '店员';

        if ($this->attributes['is_root'] > 0) {
            $root_name = '店长';
        }

        return $root_name;
    }

    public function dianpu()
    {
        return $this->belongsTo(dianpuModel::class, 'dianpu_id', 'id')->select(['id', 'name', 'company_name', 'phone']);
    }

    public function member()
    {
        return $this->belongsTo(\memberModel::class, 'member_id', 'id')->select(['id', 'phone', 'nickname', 'realname', 'avatar']);
    }

    public function roles()
    {
        return $this->belongsTo(merchantrolesModel::class, 'roles_id', 'id')->select(['id', 'name']);
    }
}